/******************************************************************
Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
* @projectName   15_videoplayer
* @brief         mainwindow.cpp
* @author        Deng Zhimao
* @email         1252699831@qq.com
* @net           www.openedv.com
* @date          2021-04-27
*******************************************************************/
#include "mainwindow.h"
#include "Can_Message.h"
#include "canMsg.h"
#include <QCoreApplication>
#include <QFileInfoList>
#include <QDir>
#include <QDebug>
#include <QProcess>


/************** macros for can *************************/
#define ip_cmd_set_can0_params "ip link set can0 type can bitrate 250000 "
#define ip_cmd_can0_up         "ip link set can0 up"
#define ip_cmd_can0_down       "ip link set can0 down"

#ifdef CRCC
    #define Group1_ID1 0x19A71F01
    #define Group1_ID2 0x19A71F02
    #define Group1_ID3 0x19A71F03
#endif

#ifdef RV_DEMON
#define Group1_ID1 0x7EA  //uds reponse from MCU Board
unsigned char backlightValue = 10;      //inital value of backlight in percent

#endif

#ifdef SHANQI
#define Group1_ID1 0x7EA  //uds reponse from MCU Board
unsigned char backlightValue = 10;      //inital value of backlight in percent

#endif

/*******************************************************/


extern int CAN_Init(const char *pcCanName, E_CAN_BAUDRATE iBaudrateIndex, E_CAN_TYPE iCanType);
extern int CAN_SetFilter(int iCanfd, int iCanId);
extern void CAN_Uninit(int iCanFd);
extern int CAN_SendMsg(int iCanFd, T_CAN_MSG *ptCanMsg);
extern int CAN_RecvMsg(int iCanFd, T_CAN_MSG *ptCanMsg);
extern int CAN_RecvMsgTimeout(int iCanFd, T_CAN_MSG *ptCanMsg, int ms);

int MainWindow::canInit(const char *pcCanName, E_CAN_BAUDRATE iBaudrateIndex, E_CAN_TYPE iCanType)
{
    int can_fd = CAN_Init(pcCanName, iBaudrateIndex, iCanType);

    qDebug() << "can_fd:" << can_fd;

    CAN_SetFilter(can_fd, Group1_ID1);
//    CAN_SetFilter(can_fd, Group1_ID2);
//    CAN_SetFilter(can_fd, Group1_ID3);

    return can_fd;
}


int MainWindow::canRecv( int can_fd, T_CAN_MSG *frame, uint32_t timeout_ms)
{
    int recv  = CAN_RecvMsgTimeout(can_fd, frame, timeout_ms);

    return recv;

}

int MainWindow::canSend(int can_fd, T_CAN_MSG* can_send_frame)
{
    int send = CAN_SendMsg(can_fd, can_send_frame);

    return send;

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 视频播放器布局初始化 */
    videoLayout();

    /* 媒体初始化 */
    mediaPlayerInit();

    /* 扫描本地视频 */
    scanVideoFiles();
    int i = 1000;
    while(--i);

    videoPlayer->play();

    /* 设置按钮的属性 */
    pushButton[0]->setCheckable(true);
    pushButton[4]->setCheckable(true);

    /* 按钮连接信号槽 */
#ifndef SHANQI
    connect(pushButton[0], SIGNAL(clicked()),
            this, SLOT(btn_playsetting_clicked()));
#else
    //show shanqi logo
#endif
    connect(pushButton[1], SIGNAL(clicked()),
            this, SLOT(btn_backlight_add_clicked()));
    connect(pushButton[2], SIGNAL(clicked()),
            this, SLOT(btn_heightdown_clicked()));
    connect(pushButton[3], SIGNAL(clicked()),
            this, SLOT(btn_heightup_clicked()));
    connect(pushButton[4], SIGNAL(clicked()),
            this, SLOT(btn_backlight_minus_clicked()));
    connect(pushButton[5],SIGNAL(clicked()),
            this, SLOT(btn_logo_clicked()));

    /* 列表连接信号槽 */
    connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)),
            this, SLOT(listWidgetCliked(QListWidgetItem*)));

    /* 媒体连接信号槽 */
    connect(videoPlayer,
            SIGNAL(stateChanged(QMediaPlayer::State)),
            this,
            SLOT(mediaPlayerStateChanged(QMediaPlayer::State)));
    connect(mediaPlaylist,
            SIGNAL(currentIndexChanged(int)),
            this,
            SLOT(mediaPlaylistCurrentIndexChanged(int)));
    connect(videoPlayer, SIGNAL(durationChanged(qint64)),
            this,
            SLOT(musicPlayerDurationChanged(qint64)));
    connect(videoPlayer,
            SIGNAL(positionChanged(qint64)),
            this,
            SLOT(mediaPlayerPositionChanged(qint64)));

    /* slider信号槽连接 */
    connect(durationSlider, SIGNAL(sliderReleased()),
            this, SLOT(durationSliderReleased()));
//    connect(volumeSlider, SIGNAL(sliderReleased()),
//            this, SLOT(volumeSliderReleased()));
}


void MainWindow::videoLayout()
{
    /* 设置位置与大小,这里固定为*/

    this->setGeometry(0, 0, 800, 1500);   //控制界面的窗口（播放列表，控制按钮）
    //this->setGeometry(0, 0, 800, 600);
//    this->setMinimumSize(1152, 576);
//    this->setMaximumSize(1152, 576);
    QPalette pal;
    pal.setColor(QPalette::WindowText, Qt::white);

    for (int i = 0; i < 3; i++) {
        /* 水平容器 */
        hWidget[i] = new QWidget();
        hWidget[i]->setAutoFillBackground(true);
        /* 水平布局 */
        hBoxLayout[i] = new QHBoxLayout();
    }

    for (int i = 0; i < 2; i++) {
        /* 垂直容器 */
        vWidget[i] = new QWidget();
        vWidget[i]->setAutoFillBackground(true);
        /* 垂直布局 */
        vBoxLayout[i] = new QVBoxLayout();
    }

    /* init gridwidget and gridlayout */
    gWidget = new QWidget();
    gWidget->setAutoFillBackground(true);
    GridLayout = new QGridLayout();

    for (int i = 0; i < 2; i++) {
        label[i] = new QLabel();
    }

    for (int i = 0; i < 6; i++) {
        pushButton[i] = new QPushButton();
        pushButton[i]->setMaximumSize(200, 200);
        pushButton[i]->setMinimumSize(200, 200);
    }

    /* 设置 */
    vWidget[0]->setObjectName("vWidget0");
    vWidget[1]->setObjectName("vWidget1");


    hWidget[0]->setObjectName("hWidget0");   //not used now
    hWidget[1]->setObjectName("hWidget1");
    hWidget[2]->setObjectName("hWidget2");


    pushButton[0]->setObjectName("btn_playsetting");
    pushButton[1]->setObjectName("btn_backlight_add");
    pushButton[2]->setObjectName("btn_heightdown");
    pushButton[3]->setObjectName("btn_heightup");
    pushButton[4]->setObjectName("btn_backlight_minus");
    pushButton[5]->setObjectName("btn_logo");


    QFont font;

    font.setPixelSize(18);
    label[0]->setFont(font);
    label[1]->setFont(font);

    pal.setColor(QPalette::WindowText, Qt::white);
    label[0]->setPalette(pal);
    label[1]->setPalette(pal);

    label[0]->setText("00:00");
    label[1]->setText("/00:00");

    durationSlider = new QSlider(Qt::Horizontal);
    durationSlider->setMaximumHeight(15);
    durationSlider->setObjectName("durationSlider");

//    volumeSlider = new QSlider(Qt::Horizontal);
//    volumeSlider->setRange(0, 100);
//    volumeSlider->setMaximumWidth(80);
//    volumeSlider->setObjectName("volumeSlider");
//    volumeSlider->setValue(50);

    listWidget = new QListWidget();
    listWidget->setObjectName("listWidget");
    listWidget->setVerticalScrollBarPolicy(
                Qt::ScrollBarAlwaysOff);
    listWidget->setHorizontalScrollBarPolicy(
                Qt::ScrollBarAlwaysOff);


    //listWidget->setFocusPolicy(Qt::NoFocus);
    videoWidget = new QVideoWidget();
    videoWidget->setStyleSheet("border-image: none;"
                               "background: transparent;"
                               "border:none");
    videoWidget->setAttribute(Qt::WA_OpaquePaintEvent);


    /***************************************************************************************************************************************/
    videoWidget->setMinimumSize(500, 480);
    //videoWidget->setGeometry(1920,0,0,0);
    videoWidget->setGeometry(1280,0,0,0);  //视频播放的窗口
#ifdef CRCC
    //videoWidget->resize(1920,1080);     //fullscreen
    videoWidget->resize(1152,576);        //for dlpc
#endif

#ifdef  RV_DEMON
    videoWidget->resize(1920,1080);
#endif

#ifdef LIL8
    videoWidget->resize(1920,1080);  //LI L8 fixed 800*480
#endif

    //videoWidget->setStyleSheet("background-color:black;");
#ifdef SHANQI
    //just CAN Function alvilable
#endif

#ifndef SHANQI

    QWidget* backgroundWidget = new QWidget();
    backgroundWidget->setStyleSheet("border-image:url(:/images/bg.png);");
    backgroundWidget->setGeometry(800,0,1920,1080);
    backgroundWidget->resize(1920,1080);
    backgroundWidget->setWindowFlags (Qt::Dialog |  Qt::FramelessWindowHint);
    backgroundWidget->show();


//    // 2.界面美化
//    QPalette qplte;// 调色板
//    qplte.setColor(QPalette::Window, Qt::black);// 透明
//    videoWidget->setPalette(qplte);// 设置窗口部件的调色板


    videoWidget->setWindowFlags (Qt::Dialog |  Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
    videoWidget->show();
#endif
    /***********************************************************************************************************************************/

    /* H0布局 */
    vWidget[0]->setMinimumSize(300, 480);
    //vWidget[0]->setMaximumWidth(300);
/************************** set size of  listwidget *************************/
    vWidget[0]->setGeometry(0,0,1280,400);  //播放进度的背景窗口
   // hBoxLayout[0]->addWidget(vWidget[1]);
    hBoxLayout[0]->addWidget(vWidget[0]);
    hBoxLayout[0]->addWidget(hWidget[2]);


    hWidget[0]->setLayout(hBoxLayout[0]);
    hBoxLayout[0]->setContentsMargins(0, 0, 0, 0);

    setCentralWidget(hWidget[0]);

    /* V0布局 */
    QSpacerItem *vSpacer0 = new
            QSpacerItem(0, 80,
                        QSizePolicy::Minimum,
                        QSizePolicy::Maximum);
#ifndef SHANQI
    vBoxLayout[0]->addWidget(listWidget);

    vBoxLayout[0]->addSpacerItem(vSpacer0);
    vBoxLayout[0]->setContentsMargins(0, 0, 0, 0);

    vWidget[0]->setLayout(vBoxLayout[0]);
#endif

    /* V1布局 */
    /* 底板部件布局 */
    hWidget[1]->setMaximumHeight(350);
    hWidget[2]->setMinimumHeight(65);
    hWidget[2]->setGeometry(800,0,350,350);
    hWidget[2]->raise();

    vBoxLayout[1]->addWidget(hWidget[1]);
    vBoxLayout[1]->setAlignment(Qt::AlignCenter);


    vWidget[1]->setLayout(vBoxLayout[1]);
#ifdef SHANQI
    vWidget[1]->setGeometry(0, 0, 1080, 1800);
#else
    //vWidget[1]->setGeometry(0, this->width() - 50, this->height(), 100);
    vWidget[1]->setGeometry(0, this->width() - 250, this->height(), 100);
#endif
    vWidget[1]->resize(1280,200);  //播放进度条

    vWidget[1]->setParent(this);

    //test2023

    vBoxLayout[1]->setContentsMargins(0, 0, 0, 0);
    /* 位于最上层 */
    vWidget[1]->raise();

    /* H1布局 */
    hBoxLayout[1]->addWidget(pushButton[0]);

#ifdef SHANQI
    pushButton[0]->setMaximumSize(800, 200);
    pushButton[0]->setMinimumSize(800, 200);
#endif

#ifndef SHANQI
    hBoxLayout[1]->addWidget(durationSlider);
    hBoxLayout[1]->addSpacing(20);
    hBoxLayout[1]->addWidget(label[0]);
    hBoxLayout[1]->addWidget(label[1]);
#endif

    hBoxLayout[1]->setContentsMargins(0, 0, 0, 0);
    hWidget[1]->setLayout(hBoxLayout[1]);
#ifdef SHANQI
    vWidget[1]->resize(900,1080);
#else
    vWidget[1]->resize(1258,300);
#endif

    /* H2布局 */
    QSpacerItem *hSpacer0 = new
            QSpacerItem(300, 80,
                        QSizePolicy::Expanding,
                        QSizePolicy::Maximum);

    GridLayout->setSpacing(20);
    GridLayout->addWidget(pushButton[3],0,1);
    GridLayout->addWidget(pushButton[4],1,0);
    GridLayout->addWidget(pushButton[1],1,2);
    GridLayout->addWidget(pushButton[2],2,1);
    GridLayout->addWidget(pushButton[5],1,1);
    hWidget[2]->setLayout(GridLayout);

}

void MainWindow::mediaPlayerInit()
{
    videoPlayer = new QMediaPlayer(this);
    mediaPlaylist = new QMediaPlaylist(this);
    /* 确保列表是空的 */
    mediaPlaylist->clear();
    /* 设置视频播放器的列表为mediaPlaylist */
    videoPlayer->setPlaylist(mediaPlaylist);

    /* 设置视频输出窗口 */
    videoPlayer->setVideoOutput(videoWidget);

    /* 设置播放模式，Loop是列循环 */

#ifndef SHANQI
    mediaPlaylist->setPlaybackMode(QMediaPlaylist::Loop);
    // pushButton[0]->setStyleSheet(//"QPushButton{border-image:url(:/icon/right.png)}");
    //                              "QPushButton:hover{border-image:url(:/icon/stop_next.png)}");
#else
    pushButton[0]->setStyleSheet("QPushButton{border-image:url(:/icon/SQLogo.png)}");
#endif
    /* 设置默认软件音量为50% */
    //videoPlayer->setVolume(50);
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    //vWidget[1]->setGeometry(0, this->height() - 80, this->width(), 80);
}

void MainWindow::btn_playsetting_clicked()
{
    videoPlayer->stop();
    qDebug() << "playbackMode before change : " << mediaPlaylist->playbackMode();

    if(mediaPlaylist->playbackMode() == QMediaPlaylist::CurrentItemInLoop)
    {
        mediaPlaylist->setPlaybackMode(QMediaPlaylist::Loop);
        pushButton[0]->setStyleSheet("QPushButton{border-image:url(:/icon/right.png)}");
                                      //"QPushButton:hover{border-image:url(:/icon/right.png)}");    videoWidget->setWindowFlags (Qt::Dialog |  Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);

        int i = 1000;
        while(--i);

        videoPlayer->play();
    }
    else if(mediaPlaylist->playbackMode() == QMediaPlaylist::Loop)
    {
        mediaPlaylist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
        pushButton[0]->setStyleSheet("QPushButton{border-image:url(:/icon/stop_next.png)}");
                                    //"QPushButton:hover{border-image:url(:/icon/right.png)}");
        int i = 1000;
        while(--i);
        videoPlayer->play();
    }

    qDebug() << "playbackMode after change : " << mediaPlaylist->playbackMode();
}



/**
* @brief HUD status information message pack to send
* @note
* @param [in] send_data the pointer of send data array
* @param [out] can_msg The real value for message
* @retval none
*/
void Message_HUD_Status_UnPack(hud_status_msg* can_msg, hud_status_val_data* hud_status_val)
{
    for (int var = 0; var < 8; ++var)
     {
        hud_status_val->data[var] = can_msg->data[8];
    }
}


/** suspend for recv message **/
T_CAN_MSG* ptCanMsg = new T_CAN_MSG();
T_CAN_MSG ptSendMsg = {0};
tscn_ctr_msg ctrlSendMsg = {0};
hud_status_val_data hud_status_val = {0};


void MainWindow::btn_backlight_add_clicked()
{
    /** suspend for recv message **/
//    T_CAN_MSG ptCanMsg = {0};
//    T_CAN_MSG ptSendMsg = {0};
//    tscn_ctr_msg ctrlSendMsg = {0};
//    hud_status_val hud_status_val = {0};

    qDebug() << "btn_backlight_add_clicked";
#ifdef CRCC
    can_fd = canInit("can0", E_CAN_250K, E_CAN);
#endif

#ifdef RV_DEMON
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif

#ifdef SHANQI
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
    qDebug() << can_fd;
#endif

#ifdef CRCC
    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        qDebug() << "CAN_RecvMsgTimeout(can_fd, ptCanMsg, 1000) > 0";
        if( ptCanMsg->uiCanId == 0x19A71F01)
        {
            for (int var = 0; var < (UINT8)ptCanMsg->cLen; var++)
            {
                hud_status_val.data[var] = (UINT8)ptCanMsg->acData[var];
                qDebug() << var << ":" << QString().sprintf("%4X",ptCanMsg->acData[var]) << endl;
            }

            ctrlSendMsg.msg_info.RollingCounter[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.RollingCounter[1] = (UINT8)0xff;
            ctrlSendMsg.msg_info.HUD_Switch = (UINT8)0x01;                                                  //HUD display on
            ctrlSendMsg.msg_info.AutoHudBrightSet = (UINT8)0x01;


            hud_status_val.msg_info.HudBrightLevel = hud_status_val.msg_info.HudBrightLevel + 1;
            if(hud_status_val.msg_info.HudBrightLevel > 10 && hud_status_val.msg_info.HudBrightLevel <=0xFE)
            {
                hud_status_val.msg_info.HudBrightLevel = 10;
            }
            if(hud_status_val.msg_info.HudBrightLevel == 0xFF )
            {
                hud_status_val.msg_info.HudBrightLevel = 0;
            }

            ctrlSendMsg.msg_info.HudBrightAdjmtReq = (UINT8) hud_status_val.msg_info.HudBrightLevel;
            ctrlSendMsg.msg_info.HudHeiAdjmtReq = (UINT8) hud_status_val.msg_info.HudHeightLevel;

            ctrlSendMsg.msg_info.LightSensorVal[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.LightSensorVal[1] = (UINT8)0xff;

            for(int i=0; i < (UINT8)ptCanMsg->cLen; i++)
            {
                ptSendMsg.acData[i] = (char)ctrlSendMsg.data[i];
            }
            ptSendMsg.uiCanId = (UINT32)0x191FA700;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = (UINT8)ptCanMsg->cIde;
            ptSendMsg.cRtr = (UINT8)ptCanMsg->cRtr;
            canSend(can_fd, &ptSendMsg);
        }
    }
#endif

#ifdef RV_DEMON
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = (UINT8)CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = (UINT8)CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    int ret = canSend(can_fd, &ptSendMsg);
    qDebug() << "ret = " << ret;

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            backlightValue += 5;         // click on for 5 added
            qDebug() << "backlightValue = " << backlightValue;
            if(backlightValue >= 100)
            {
                backlightValue = 100;
                qDebug() << "The brightness has reached 100, and the button operation has failed";
            }

            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x07;
            ptSendMsg.acData[5] = (UINT8)backlightValue;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x00;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "backlight added ok";
                }
                else
                {
                    qDebug() << "backlight added error";
                }
            }
            else
            {
                qDebug() << "backlight added timeout";
            }
            // if(backlightValue >= 100)
            // {
            //     backlightValue = 100;
            //     qDebug() << "backlightValue = " << backlightValue;

            // }

        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif

#ifdef SHANQI
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = (UINT8)CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = (UINT8)CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    int ret = canSend(can_fd, &ptSendMsg);
    qDebug() << "ret = " << ret;


    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 5000))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            backlightValue += 5;         // click on for 5 added
            qDebug() << "backlightValue = " << backlightValue;
            if(backlightValue >= 100)
            {
                backlightValue = 100;
                qDebug() << "The brightness has reached 100, and the button operation has failed";
            }

            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x07;
            ptSendMsg.acData[5] = (UINT8)backlightValue;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x00;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "backlight added ok";
                }
                else
                {
                    qDebug() << "backlight added error";
                }
            }
            else
            {
                qDebug() << "backlight added timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif
}



void MainWindow::btn_backlight_minus_clicked()
{
    /** suspend for recv message **/
    qDebug() << "btn_backlight_minus_clicked";
#ifdef CRCC
    can_fd = canInit("can0", E_CAN_250K, E_CAN);
#endif

#ifdef RV_DEMON
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif
#ifdef SHANQI
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif


#ifdef CRCC
    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        qDebug() << "CAN_RecvMsgTimeout(can_fd, ptCanMsg, 1000) > 0";
        if( ptCanMsg->uiCanId == 0x19A71F01)
        {
            for (int var = 0; var < (UINT8)ptCanMsg->cLen; var++)
            {
                hud_status_val.data[var] = (UINT8)ptCanMsg->acData[var];
                qDebug() << var << ":" << QString().sprintf("%4X",ptCanMsg->acData[var]) << endl;

            }

            ctrlSendMsg.msg_info.RollingCounter[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.RollingCounter[1] = (UINT8)0xff;
            ctrlSendMsg.msg_info.HUD_Switch = (UINT8)0x01;                                                  //HUD display on
            ctrlSendMsg.msg_info.AutoHudBrightSet = (UINT8)0x01;


            hud_status_val.msg_info.HudBrightLevel = hud_status_val.msg_info.HudBrightLevel - 1;
            if(hud_status_val.msg_info.HudBrightLevel > 10 && hud_status_val.msg_info.HudBrightLevel <=0xFE)
            {
                hud_status_val.msg_info.HudBrightLevel = 10;
            }
            if(hud_status_val.msg_info.HudBrightLevel == 0xFF)
            {
                hud_status_val.msg_info.HudBrightLevel = 0;
            }

            ctrlSendMsg.msg_info.HudBrightAdjmtReq = (UINT8) hud_status_val.msg_info.HudBrightLevel;
            ctrlSendMsg.msg_info.HudHeiAdjmtReq = (UINT8) hud_status_val.msg_info.HudHeightLevel;

            ctrlSendMsg.msg_info.LightSensorVal[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.LightSensorVal[1] = (UINT8)0xff;

            for(int i=0; i < (UINT8)ptCanMsg->cLen; i++)
            {
                ptSendMsg.acData[i] = (char)ctrlSendMsg.data[i];
            }
            ptSendMsg.uiCanId = (UINT32)0x191FA700;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = (UINT8)ptCanMsg->cIde;
            ptSendMsg.cRtr = (UINT8)ptCanMsg->cRtr;
            canSend(can_fd, &ptSendMsg);
        }
    }
#endif


#ifdef RV_DEMON
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    canSend(can_fd, &ptSendMsg);

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            backlightValue -= 5;         // click on for 5 added
            qDebug() << "backlightValue = " << backlightValue;
            if(backlightValue >= 100)
            {
                backlightValue = 0;
                qDebug() << "The brightness has reached 0, and the button operation has failed";
            }

            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x07;
            ptSendMsg.acData[5] = (UINT8)backlightValue;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x00;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "backlight minus ok";
                }
                else
                {
                    qDebug() << "backlight minus error";
                }
            }
            else
            {
                qDebug() << "backlight minus timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif

#ifdef SHANQI
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    canSend(can_fd, &ptSendMsg);

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            backlightValue -= 5;         // click on for 5 added
            qDebug() << "backlightValue = " << backlightValue;
            if(backlightValue >= 100)
            {
                backlightValue = 0;
                qDebug() << "The brightness has reached 0, and the button operation has failed";
            }

            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x07;
            ptSendMsg.acData[5] = (UINT8)backlightValue;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x00;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "backlight minus ok";
                }
                else
                {
                    qDebug() << "backlight minus error";
                }
            }
            else
            {
                qDebug() << "backlight minus timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif
}

void MainWindow::btn_heightup_clicked()
{
    /** suspend for recv message **/
//    T_CAN_MSG ptCanMsg;
//    T_CAN_MSG ptSendMsg;
//    tscn_ctr_msg ctrlSendMsg = {0};
//    hud_status_val hud_status_val = {0};

    qDebug() << "btn_heightup_clicked";
    /**************can init *******************/
#ifdef CRCC
    can_fd = canInit("can0", E_CAN_250K, E_CAN);
#endif

#ifdef RV_DEMON
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif
#ifdef SHANQI
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif

#ifdef CRCC
    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 100))
    {
        qDebug() << "CAN_RecvMsgTimeout(can_fd, ptCanMsg, 1000) > 0";
        if( ptCanMsg->uiCanId == 0x19A71F01)
        {
            for (int var = 0; var < (UINT8)ptCanMsg->cLen; var++)
            {
                hud_status_val.data[var] = ptCanMsg->acData[var];
                qDebug() << var << ":" << QString().sprintf("%4X",ptCanMsg->acData[var]) << endl;
            }

            ctrlSendMsg.msg_info.RollingCounter[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.RollingCounter[1] = (UINT8)0xff;
            ctrlSendMsg.msg_info.HUD_Switch = (UINT8)0x01;                                                  //HUD display on
            ctrlSendMsg.msg_info.AutoHudBrightSet = (UINT8)0x01;

            hud_status_val.msg_info.HudHeightLevel = hud_status_val.msg_info.HudHeightLevel + 1;

            if(hud_status_val.msg_info.HudHeightLevel > 6 && hud_status_val.msg_info.HudHeightLevel <= 0xFE)
            {
                hud_status_val.msg_info.HudHeightLevel = 6;
            }
            if(hud_status_val.msg_info.HudHeightLevel == 0xFF)
            {
                hud_status_val.msg_info.HudHeightLevel = 0;
            }

            ctrlSendMsg.msg_info.HudHeiAdjmtReq = (UINT8) hud_status_val.msg_info.HudHeightLevel;

            ctrlSendMsg.msg_info.HudBrightAdjmtReq = (UINT8) hud_status_val.msg_info.HudBrightLevel;

            ctrlSendMsg.msg_info.LightSensorVal[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.LightSensorVal[1] = (UINT8)0xff;

            for(int i=0; i < (UINT8)ptCanMsg->cLen; i++)
            {
                ptSendMsg.acData[i] = (char)ctrlSendMsg.data[i];
            }
            ptSendMsg.uiCanId = (UINT32)0x191FA700;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = (UINT8)ptCanMsg->cIde;
            ptSendMsg.cRtr = (UINT8)ptCanMsg->cRtr;
            canSend(can_fd, &ptSendMsg);

            system(ip_cmd_can0_down);

        }
    }
#endif


#ifdef RV_DEMON
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    canSend(can_fd, &ptSendMsg);

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x05;
            ptSendMsg.acData[5] = (UINT8)0x03;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x05;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "height up ok";
                }
                else
                {
                    qDebug() << "height up error";
                }
            }
            else
            {
                qDebug() << "height up timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif

#ifdef SHANQI
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    canSend(can_fd, &ptSendMsg);

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x05;
            ptSendMsg.acData[5] = (UINT8)0x03;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x05;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "height up ok";
                }
                else
                {
                    qDebug() << "height up error";
                }
            }
            else
            {
                qDebug() << "height up timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif
}


void MainWindow::btn_heightdown_clicked()
{

    /** suspend for recv message **/
//    T_CAN_MSG ptCanMsg;
//    T_CAN_MSG ptSendMsg;
//    tscn_ctr_msg ctrlSendMsg = {0};
//    hud_status_val hud_status_val = {0};
    qDebug() << "btn_heightdown_clicked";
    //system(ip_cmd_can0_up);
    /**************can init *******************/
#ifdef CRCC
    can_fd = canInit("can0", E_CAN_250K, E_CAN);
#endif

#ifdef RV_DEMON
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif
#ifdef SHANQI
    can_fd = canInit("can0", E_CAN_500K, E_CAN);
#endif

#ifdef CRCC
    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 100))
    {
        qDebug() << "CAN_RecvMsgTimeout(can_fd, ptCanMsg, 1000) > 0";
        if( ptCanMsg->uiCanId == 0x19A71F01)
        {
            for (int var = 0; var < (UINT8)ptCanMsg->cLen; var++)
            {
                hud_status_val.data[var] = ptCanMsg->acData[var];
                qDebug() << var << ":" << QString().sprintf("%4X",ptCanMsg->acData[var]) << endl;
            }

            ctrlSendMsg.msg_info.RollingCounter[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.RollingCounter[1] = (UINT8)0xff;
            ctrlSendMsg.msg_info.HUD_Switch = (UINT8)0x01;                                                  //HUD display on
            ctrlSendMsg.msg_info.AutoHudBrightSet = (UINT8)0x01;


            hud_status_val.msg_info.HudHeightLevel = hud_status_val.msg_info.HudHeightLevel - 1;
            if(hud_status_val.msg_info.HudHeightLevel > 6 && hud_status_val.msg_info.HudHeightLevel <= 0xFE)
            {
                hud_status_val.msg_info.HudHeightLevel = 6;
            }
            if(hud_status_val.msg_info.HudHeightLevel == 0xFF)
            {
                hud_status_val.msg_info.HudHeightLevel = 0;
            }

            ctrlSendMsg.msg_info.HudHeiAdjmtReq = (UINT8) hud_status_val.msg_info.HudHeightLevel;

            ctrlSendMsg.msg_info.HudBrightAdjmtReq = (UINT8) hud_status_val.msg_info.HudBrightLevel;

            ctrlSendMsg.msg_info.LightSensorVal[0] = (UINT8)0x01;
            ctrlSendMsg.msg_info.LightSensorVal[1] = (UINT8)0xff;

            for(int i=0; i < (UINT8)ptCanMsg->cLen; i++)
            {
                ptSendMsg.acData[i] = (char)ctrlSendMsg.data[i];
            }
            ptSendMsg.uiCanId = (UINT32)0x191FA700;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = (UINT8)ptCanMsg->cIde;
            ptSendMsg.cRtr = (UINT8)ptCanMsg->cRtr;
            canSend(can_fd, &ptSendMsg);
            system(ip_cmd_can0_down);
        }
    }
#endif


#ifdef RV_DEMON
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    canSend(can_fd, &ptSendMsg);

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x05;
            ptSendMsg.acData[5] = (UINT8)0x02;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x05;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "height down ok";
                }
                else
                {
                    qDebug() << "height down error";
                }
            }
            else
            {
                qDebug() << "height down timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif


#ifdef SHANQI
    // 10 03
    ptSendMsg.uiCanId = (UINT32)0x7E2;
    ptSendMsg.cLen = (UINT8)8;
    ptSendMsg.cIde = CAN_TYPE_STANDARD;
    ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
    ptSendMsg.acData[0] = (UINT8)0x02;
    ptSendMsg.acData[1] = (UINT8)0x10;
    ptSendMsg.acData[2] = (UINT8)0x03;
    canSend(can_fd, &ptSendMsg);

    if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
    {
        if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x50))   //check in 10 03
        {
            ptSendMsg.uiCanId = (UINT32)0x7E2;
            ptSendMsg.cLen = (UINT8)8;
            ptSendMsg.cIde = CAN_TYPE_STANDARD;
            ptSendMsg.cRtr = CAN_FRAMETYPE_NORMAL;
            ptSendMsg.acData[0] = (UINT8)0x07;
            ptSendMsg.acData[1] = (UINT8)0x31;
            ptSendMsg.acData[2] = (UINT8)0x01;
            ptSendMsg.acData[3] = (UINT8)0x84;
            ptSendMsg.acData[4] = (UINT8)0x05;
            ptSendMsg.acData[5] = (UINT8)0x02;
            ptSendMsg.acData[6] = (UINT8)0x00;
            ptSendMsg.acData[7] = (UINT8)0x05;
            canSend(can_fd, &ptSendMsg);

            if(CAN_RecvMsgTimeout(can_fd, ptCanMsg, 500))
            {
                if( (ptCanMsg->uiCanId == 0x7EA) && (ptCanMsg->acData[1] == 0x71))   //check in 10 03
                {
                    qDebug() << "height down ok";
                }
                else
                {
                    qDebug() << "height down error";
                }
            }
            else
            {
                qDebug() << "height down timeout";
            }
        }
        else
        {
            qDebug() << "10 03 negative";
        }
    }
#endif
}


void MainWindow::btn_logo_clicked()
{
    qDebug() << "btn_logo_clicked";
    //char acCmd[64];
    QString command = QString( "/bin/sh -c \"echo \"\"\"output:HDMI-A-1:rotate180 \"\"\" > /tmp/.weston_drm.conf");

    QProcess::execute(command);

    //sprintf(acCmd,"echo \"output:HDMI-A-1:rotate180\" > \/tmp\/.weston_drm.conf");
}

void MainWindow::mediaPlayerStateChanged(
        QMediaPlayer::State
        state)
{
    switch (state) {
    case QMediaPlayer::StoppedState:
        {
            pushButton[0]->setChecked(false);

            //
            videoPlayer->stop();
            qDebug() << "media end!";
            int i = 1000;
            while(--i);
            videoPlayer->play();
        }
        break;

    case QMediaPlayer::PlayingState:
        pushButton[0]->setChecked(true);
        break;

    case QMediaPlayer::PausedState:
        pushButton[0]->setChecked(false);
        break;
    }
}

void MainWindow::listWidgetCliked(QListWidgetItem *item)
{
    videoPlayer->stop();
    mediaPlaylist->setCurrentIndex(listWidget->row(item));
    int i = 1000;
    while(--i);

    videoPlayer->play();

//    videoPlayer->setVideoOutput(videoWidget);
//    videoWidget->setWindowFlags (Qt::Dialog |  Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
}

void MainWindow::mediaPlaylistCurrentIndexChanged(
        int index)
{
    if (-1 == index)
        return;

    /* 设置列表正在播放的项 */
    listWidget->setCurrentRow(index);

    videoPlayer->setVideoOutput(videoWidget);
    videoWidget->setWindowFlags (Qt::Dialog |  Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
    //videoWidget->setAttribute(Qt::WA_OpaquePaintEvent);

    videoPlayer->stop();
    qDebug() << "media end!" << endl;
    int i = 1000;
    while(--i);
    videoPlayer->play();
}

void MainWindow::musicPlayerDurationChanged(
        qint64 duration)
{
    durationSlider->setRange(0, duration / 1000);
    int second  = duration / 1000;
    int minute = second / 60;
    second %= 60;

    QString mediaDuration;
    mediaDuration.clear();

    if (minute >= 10)
        mediaDuration = QString::number(minute, 10);
    else
        mediaDuration = "0" + QString::number(minute, 10);

    if (second >= 10)
        mediaDuration = mediaDuration
                + ":" + QString::number(second, 10);
    else
        mediaDuration = mediaDuration
                + ":0" + QString::number(second, 10);

    /* 显示媒体总长度时间 */
    label[1]->setText("/" + mediaDuration);
}

void MainWindow::mediaPlayerPositionChanged(
        qint64 position)
{
    if (!durationSlider->isSliderDown())
        durationSlider->setValue(position / 1000);

    int second  = position / 1000;
    int minute = second / 60;
    second %= 60;

    QString mediaPosition;
    mediaPosition.clear();

    if (minute >= 10)
        mediaPosition = QString::number(minute, 10);
    else
        mediaPosition = "0" + QString::number(minute, 10);

    if (second >= 10)
        mediaPosition = mediaPosition
                + ":" + QString::number(second, 10);
    else
        mediaPosition = mediaPosition
                + ":0" + QString::number(second, 10);

    /* 显示现在播放的时间 */
    label[0]->setText(mediaPosition);
}

void MainWindow::durationSliderReleased()
{
    /* 设置媒体播放的位置 */
    videoPlayer->setPosition(durationSlider->value() * 1000);
}

void MainWindow::volumeSliderReleased()
{
    /* 设置音量 */
    //videoPlayer->setVolume(volumeSlider->value());
}

void MainWindow::scanVideoFiles()
{
    QDir dir(QCoreApplication::applicationDirPath()
             + "/myVideo");
    QDir dirbsolutePath(dir.absolutePath());
    /* 如果目录存在 */
    if (dirbsolutePath.exists()) {
        /* 定义过滤器 */
        QStringList filter;
        /* 包含所有xx后缀的文件 */
        filter << "*.mp4" << "*.mkv" << "*.wmv" << "*.avi";
        /* 获取该目录下的所有文件 */
        QFileInfoList files =
                dirbsolutePath.entryInfoList(filter, QDir::Files);
        /* 遍历 */
        for (int i = 0; i < files.count(); i++) {
            MediaObjectInfo info;

            /* 使用utf-8编码 */
            info.fileName = QString::fromUtf8(files.at(i)
                                              .fileName()
                                              .toUtf8()
                                              .data());
            info.filePath = QString::fromUtf8(files.at(i)
                                              .filePath()
                                              .toUtf8()
                                              .data());
            /* 媒体列表添加视频 */
            if (mediaPlaylist->addMedia(
                        QUrl::fromLocalFile(info.filePath))) {
                /* 添加到容器数组里储存 */
                mediaObjectInfo.append(info);
                /* 添加视频名字至列表 */
                listWidget->addItem(info.fileName);
                listWidget->setStyleSheet("QListWidget{font-size:66px;}");
            } else {
                qDebug()<<
                           mediaPlaylist->errorString()
                           .toUtf8().data()
                        << endl;
                qDebug()<< "  Error number:"
                        << mediaPlaylist->error()
                        << endl;
            }
        }
    }
}

MainWindow :: ~MainWindow()
{
    //delete(&can_fd);
    system(ip_cmd_can0_down);
}


